/**
 * 
 */
package soa.security.test.ws;

import java.io.IOException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.junit.Assert;
import org.junit.Test;

/**
 * @author Cre.Gu
 * 
 */
public class AccountServiceClient extends WebServiceClient {
	protected static final org.slf4j.Logger log = org.slf4j.LoggerFactory
			.getLogger(AccountServiceClient.class);

	public static final String WSURL = BASE_WSDL + "AccountService?wsdl";

	public Boolean initialize(String organizationId, String account) {
		try {
			String method = "initialize";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("account", XMLType.XSD_STRING, ParameterMode.IN);
			call.setReturnType(XMLType.XSD_BOOLEAN);

			Object obj = call.invoke(new Object[] { organizationId, account });
			Boolean retData = (Boolean) obj;
			return retData;
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return null;
		}
	}

	public String allAccounts(String organizationId) {
		try {
			String method = "allAccounts";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);

			Object obj = call.invoke(new Object[] { organizationId });
			String retData = (String) obj;
			return retData;
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return null;
		}
	}

	public Boolean authenticate(String uid, String password) {
		try {
			String method = "authenticate";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("uid", XMLType.XSD_STRING, ParameterMode.IN);
			call.addParameter("password", XMLType.XSD_STRING, ParameterMode.IN);
			call.setReturnType(XMLType.XSD_BOOLEAN);

			return (Boolean) call.invoke(new Object[] { uid, password });
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public Boolean bind(String organizationId, String accountId, String personId) {
		try {
			String method = "bind";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("accountId", XMLType.XSD_STRING, ParameterMode.IN);
			call.addParameter("personId", XMLType.XSD_STRING, ParameterMode.IN);
			call.setReturnType(XMLType.XSD_BOOLEAN);

			return (Boolean) call.invoke(new Object[] { organizationId,
					accountId, personId });
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Test
	public void tesetIn() throws IOException {
		String account = WebServiceClient.readXml("account.xml");
		System.out.println(account);
		// initialize(ORGANIZATIONID, account);
	}

	@Test
	public void tesetAll() throws IOException {
		System.out.println(allAccounts(ORGANIZATIONID));
	}

	@Test
	public void testAuthenticate() {
		Assert.assertTrue(authenticate("zl@ZJTZSWXXJSYXGS", "888888"));
		Assert.assertTrue(authenticate("gcb@ZJTZSWXXJSYXGS", "123456"));
	}

	@Test
	public void testBind() {
		Assert.assertTrue(bind(ORGANIZATIONID, "gcb@ZJTZSWXXJSYXGS", "PE0009"));
	}
}
